//
//  NSFlashChatViewController.m
//  Meboo
//
//  Created by kyo on 2024/3/19.
//

#import "NSFlashChatViewController.h"
#import "NSConversationViewController.h"
#import "NSModelDetailViewController.h"
#import "KLVipViewController.h"
#import "NSFCPopView.h"
@interface NSFlashChatViewController ()
@property(nonatomic,strong) UIImageView *backImageView;
@property (nonatomic, strong) UIView *backView;
@property(nonatomic,strong) UIButton *closeBtn;
@property(nonatomic,strong) UIButton *chatBtn;

@property (nonatomic, strong) UILabel *titleLab;
@property (nonatomic, strong) UILabel *subTitleLab;
//fc购买弹窗
@property (nonatomic, strong) NSFCPopView *fcPopView;
@end

@implementation NSFlashChatViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = HEXS(@"ffffff");
    self.navigationController.navigationBarHidden = YES;
    [self setupUI];
    
    NSMutableArray *marr = [[NSMutableArray alloc]initWithArray:self.navigationController.viewControllers];
    for (BaseViewController *vc in marr) {
        if ([vc isKindOfClass:[NSModelDetailViewController class]]) {
            [marr removeObject:vc];
            break;
        }
    }
    self.navigationController.viewControllers = marr;
}

- (UIStatusBarStyle)preferredStatusBarStyle {
    return UIStatusBarStyleLightContent;
}

#pragma mark - Private Method
- (void)setStyle:(fcFromStyle)style {
    _style = style;
}

- (void)setModel:(NSUserInfoModel *)model {
    _model = model;
    [self.backImageView sd_setImageWithURL:[NSURL URLWithString:model.avatarUrl]];
    self.titleLab.text = [NSString stringWithFormat:@"Ready for an exciting chat witch %@ Start a flash chat now!",model.nickName];

    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:self.titleLab.text];

    //搜索字符串中指定字符串的第一次出现的范围
    NSRange nick1Range = [[attributedString string] rangeOfString:model.nickName];
    [attributedString addAttribute:NSForegroundColorAttributeName value:HEXS(@"DEF17D") range:nick1Range];

    self.titleLab.attributedText = attributedString;
}

#pragma mark - Action
//请求fc
- (void)chatAction {
    if ([MBUserAccount defaultAccount].fcNum > 0) {
        self.chatBtn.enabled = NO;
        [MBProgressHUD showActivityMessageInWindow:@"Flash Chatting..."];
        [[MBMineDataManager sharedManager] payWithType:1 chatUserCode:self.model.userCode msgId:@"" imageCode:@"" imageUrl:@"" success:^(BOOL success) {
            [MBProgressHUD hideHUD];
            self.chatBtn.enabled = YES;
            if (success) {
                NSConversationViewController *conversationVC = [[NSConversationViewController alloc] initWithConversationType:ConversationType_PRIVATE targetId:self.model.userCode];
                conversationVC.isFlashChat = YES;
                [self.navigationController pushViewController:conversationVC animated:YES];
                NSMutableArray *marr = [[NSMutableArray alloc]initWithArray:self.navigationController.viewControllers];
                for (BaseViewController *vc in marr) {
                    if (vc == self) {
                        [marr removeObject:vc];
                        break;
                    }
                }
                self.navigationController.viewControllers = marr;
                NSNotification *notify = [NSNotification notificationWithName:HOME_MODELDETAIL_LIKEPASS object:@"3"];
                [MBNotificationCenter postNotification:notify];//中心调方法，内容是参数
            } else {
                [MBProgressHUD showErrorMessage:@"Flash Chatting Faile"];
            }
        }];
    } else {
        [kKeyWindow addSubview:self.fcPopView];
        self.fcPopView.hidden = NO;
        NSString *ioStr = @"";
        if (self.style == HomeCellStyle) {
            ioStr = @"Home_Flashchat_Buy";
        } else if (self.style == HomeProfileStyle) {
            ioStr = @"Home_Profile_Flash_chat_Buy";
        }
        /**
         埋点
         首页点击flash chat拉起购买弹窗 Home_Flashchat_Buy
         首页-profile页点击flash chat拉起购买弹窗 Home_Profile_Flash_chat_Buy
         */
        [[NSIOApiManager sharedManager] setIOWithEventName:ioStr];
    }
}

- (void)closeAction {
//    [self.navigationController dismissViewControllerAnimated:YES completion:nil];
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - UI
- (void)setupUI {
    [self.view addSubview:self.backImageView];
    [self.backImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
    
    [self.view addSubview:self.backView];
    [self.backView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
    
    [self.view addSubview:self.closeBtn];
    [self.closeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(StatusBarHeight+MBHeightScale(12));
        make.right.mas_equalTo(MBWidthScale(-15));
        make.width.height.mas_equalTo(MBHeightScale(20));
    }];
    
    
    [self.view addSubview:self.chatBtn];
    [self.chatBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(-TabbarSafeBottomMargin-MBHeightScale(50));
        make.right.mas_equalTo(MBWidthScale(-36));
        make.left.mas_equalTo(MBWidthScale(36));
        make.height.mas_equalTo(MBHeightScale(49));
    }];
    
    
    [self.view addSubview:self.subTitleLab];
    [self.subTitleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.chatBtn.mas_top).offset(-MBHeightScale(40));
        make.right.mas_equalTo(MBWidthScale(-36));
        make.left.mas_equalTo(MBWidthScale(36));
    }];
    
    [self.view addSubview:self.titleLab];
    [self.titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.subTitleLab.mas_top).offset(-MBHeightScale(20));
        make.right.mas_equalTo(MBWidthScale(-36));
        make.left.mas_equalTo(MBWidthScale(36));
    }];
    
    //pop
    [self fcPopView];
}

#pragma mark - Setter && Getter
- (UIImageView *)backImageView {
    if (!_backImageView) {
        _backImageView = [UIImageView new];
        _backImageView.contentMode = UIViewContentModeScaleAspectFill;
        _backImageView.clipsToBounds = YES;
    }
    return _backImageView;
}

- (UIView *)backView {
    if (!_backView) {
        _backView = [UIView new];
        _backView.backgroundColor = [UIColor colorWithRed:0 / 255.0 green:0 / 255.0 blue:0 / 255.0 alpha:0.3];
    }
    return _backView;
}

- (UIButton *)closeBtn {
    if (!_closeBtn) {
        _closeBtn = [UIButton new];
        [_closeBtn setYXEnlargeEdgeWithTop:10 right:10 bottom:10 left:10];
        [_closeBtn setImage:ImageWithName(@"flash_chat_close") forState:0];
        [_closeBtn addTarget:self action:@selector(closeAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _closeBtn;
}

- (UIButton *)chatBtn {
    if (!_chatBtn) {
        _chatBtn = [UIButton new];
        [_chatBtn setBackgroundColor:HEXS(@"ffffff")];
        _chatBtn.layer.cornerRadius = MBHeightScale(49.0/2);
        [_chatBtn setTitle:@"Start Chatting" forState:0];
        [_chatBtn setTitleColor:HEXS(@"B92176") forState:0];
        _chatBtn.titleLabel.font = PDMediumScale(20);
        [_chatBtn addTarget:self action:@selector(chatAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _chatBtn;
}

- (UILabel *)titleLab {
    if (!_titleLab) {
        _titleLab = [UILabel new];
        _titleLab.numberOfLines = 0;
        _titleLab.textAlignment = NSTextAlignmentCenter;
        _titleLab.textColor = HEXS(@"ffffff");
        _titleLab.font = PDSemiBoldScale(30);
    }
    return _titleLab;
}

- (UILabel *)subTitleLab {
    if (!_subTitleLab) {
        _subTitleLab = [UILabel new];
        _subTitleLab.numberOfLines = 0;
        _subTitleLab.textAlignment = NSTextAlignmentCenter;
        _subTitleLab.textColor = HEXS(@"EDFF91");
        _subTitleLab.font = PDRegularScale(14);
        _subTitleLab.text = @"Let's start chatting, make new friends and start a journey of happiness";
    }
    return _subTitleLab;
}

- (NSFCPopView *)fcPopView {
    if (!_fcPopView) {
        __WeakSelf
        _fcPopView = [[NSFCPopView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight)];
        _fcPopView.payBlock = ^{
            NSString *ioStr = @"";
            if (weakSelf.style == HomeCellStyle) {
                ioStr = @"Home_Flashchat_Buysuccess";
            } else if (weakSelf.style == HomeProfileStyle) {
                ioStr = @"Home_Profile_Flash_chat_Buysuccess";
            }
            /**
             埋点
             首页点击flash chat拉起购买弹窗支付成功 Home_Flashchat_Buysuccess
             首页-profile页点击flash chat拉起购买弹窗支付成功 Home_Profile_Flash_chat_Buysuccess
             */
            [[NSIOApiManager sharedManager] setIOWithEventName:ioStr];
        };
    }
    return _fcPopView;
}
@end
